package com.sx.zongzhi.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.sx.zongzhi.bean.ProvinceBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by XZY on 2017/12/20.
 */
public class ProvinceUtils  {

    private ArrayList<ProvinceBean> provinceList=new ArrayList<>();
    private ArrayList<List<ProvinceBean>> cityList=new ArrayList<>();
    private ArrayList<List<List<ProvinceBean>>> districtList=new ArrayList<>();

    private Context context;


    public void initData(Context context ,AdapterCallback mCallback){
        this.mCallback=mCallback;
        this.context=context;
        initJson();
    }

    AdapterCallback mCallback;

    /**
     * 定义一个接口
     */
    public interface AdapterCallback {
        void getData(ArrayList<ProvinceBean> provinceList,ArrayList<List<ProvinceBean>> cityList,
                     ArrayList<List<List<ProvinceBean>>> districtList);
    }

    /*接口回调*/
    public void getType(AdapterCallback callback) {
        mCallback = callback;
    }

    private void initJson() {
        try {
            JSONArray array = new JSONArray(getJson());
            for (int i=0;i<array.length();i++) {
                Iterator iterator = array.getJSONObject(i).keys();
                while (iterator.hasNext()) {
                    String key = (String) iterator.next();//省名
                    JSONObject pObj = array.getJSONObject(i).getJSONObject(key);
                    String value = pObj.getString("val");//省id
                    ProvinceBean pBean = new ProvinceBean(key, value);
                    provinceList.add(pBean);

                    JSONObject cityObj=pObj.getJSONObject("items");//市集合对象
                    ArrayList<ProvinceBean> cityList1=new ArrayList<>();
                    ArrayList<List<ProvinceBean>> districtList1=new ArrayList<>();
                    Iterator iteratorCity = cityObj.keys();
                    while (iteratorCity.hasNext()){
                        String cityKey=(String) iteratorCity.next();//市名
                        JSONObject cObj = cityObj.getJSONObject(cityKey);//市对象
                        String cityId = cObj.getString("val");//市id
                        ProvinceBean cBean = new ProvinceBean(cityKey, cityId);
                        cityList1.add(cBean);
                        JSONObject districtObj=cObj.getJSONObject("items");//县区集合对象
                        Iterator iteratorDistrict = districtObj.keys();
                        ArrayList<ProvinceBean> districtList2=new ArrayList<>();
                        while (iteratorDistrict.hasNext()){
                            String districtKey=(String) iteratorDistrict.next();//县区名
                            String districtId=districtObj.getString(districtKey);//县区id
                            ProvinceBean dBean=new ProvinceBean(districtKey,districtId);
                            districtList2.add(dBean);
                        }
                        districtList1.add(districtList2);
                    }
                    districtList.add(districtList1);
                    cityList.add(cityList1);
                }
            }
            if (mCallback!=null){
                mCallback.getData(provinceList,cityList,districtList);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    private String getJson() {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open("province.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
